package com.yyq.sos.permission;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import com.yyq.adms.usermanage.bpo.SysUsersBpo;
import com.yyq.adms.usermanage.pojo.SysDepts;
import com.yyq.adms.usermanage.pojo.SysUsers;
import com.yyq.servlets.ApplicationContextHandle;

public class LoginUser
{
	private SysUsersBpo sysUsersBpo;
	public static ThreadLocal userThread = new ThreadLocal();
	private Long userId = new Long(0) ;
	private String userName = "System";
	private String userCname = "ϵͳ";
	private String fullName;
	private Long deptId = new Long(0);
	private String deptLevel;
	private String deptType;
	private String deptCname;
	private Permissions perms;
	private String filterIds;
	private String filterLevel;

	private String init_password;
	private Integer login_failures;
	private Integer lock_sign;
	private Date pwd_modified_time;
	private String credential_number;
	private Date login_time;
	
	private Long orgId;
	private String orgname;
	private String userType;

	private Map operationMap;
	
	private String ip;


	public String getIp() {
		return ip;
	}

	public void setIp(String ip) {
		this.ip = ip;
	}

	public LoginUser(){

	}

	public LoginUser(Long userId){
		String hql = "select a.userId,a.userName,a.userCname,b.deptId,b.deptLevel,b.deptType,b.deptCname,a.fullName," +
				"a.init_password,a.login_failures,a.lock_sign,a.pwd_modified_time,a.redential_number,a.login_time from "+SysUsers.class.getName()+" a,"+SysDepts.class.getName()+" b where a.deptId=b.deptId and a.userId="+userId;
		/*if(sysUsersBpo==null){
			WebApplicationContext wct = WebApplicationContextUtils.getWebApplicationContext(ServletActionContext.getServletContext());
			sysUsersBpo = wct.getBean("sysUsersBpo",SysUsersBpo.class);
		}*/
		Object[] objs = (Object[])sysUsersBpo.search(hql).get(0);
		this.userId = new Long(String.valueOf(objs[0]));
		this.userName= String.valueOf(objs[1]);
		this.userCname = String.valueOf(objs[2]);
		this.deptId = new Long(String.valueOf(objs[3]));
		this.deptLevel = String.valueOf(objs[4]);
		this.deptType = String.valueOf(objs[5]);
		this.deptCname = String.valueOf(objs[6]);
		this.fullName = String.valueOf(objs[7]);
		this.init_password=String.valueOf(objs[8]);
		this.login_failures=new Integer(String.valueOf(objs[9]));
		this.lock_sign=new Integer(String.valueOf(objs[10]));
		this.pwd_modified_time=(Date)objs[11];
		this.credential_number=String.valueOf(objs[12]);
		this.login_time=(Date)objs[13];

		filterDeptIds();
	}
	
	public LoginUser(String loginName){
		String hql = "select a.id,a.userName,a.userCname,b.id,b.deptLevel,b.deptType,b.deptCname,a.fullName," +
		"a.pwd_modified_time from "+SysUsers.class.getName()+" a,"+SysDepts.class.getName()+" b where a.deptId=b.id and a.userName='"+loginName+"'";
		if(sysUsersBpo==null){
			sysUsersBpo = (SysUsersBpo)ApplicationContextHandle.getBean("sysUsersBpo");
System.out.println("sysusersBpo class=====>"+sysUsersBpo.getClass());
		}
		Object[] objs = (Object[])sysUsersBpo.search(hql).get(0);
		this.userId = new Long(String.valueOf(objs[0]));
		this.userName= String.valueOf(objs[1]);
		this.userCname = String.valueOf(objs[2]);
		this.deptId = new Long(String.valueOf(objs[3]));
		this.deptLevel = String.valueOf(objs[4]);
		this.deptType = String.valueOf(objs[5]);
		this.deptCname = String.valueOf(objs[6]);
		this.fullName = String.valueOf(objs[7]);
		//this.init_password=String.valueOf(objs[8]);
		//this.login_failures=new Integer(String.valueOf(objs[9]));
		//this.lock_sign=new Integer(String.valueOf(objs[10]));
		this.pwd_modified_time=(Date)objs[8];
		//this.credential_number=String.valueOf(objs[12]);
		//this.login_time=(Date)objs[13];
		
		filterDeptIds();
	}

	private void filterDeptIds(){
		String hql = "select parent_id from ss_sys_depts where id = "+this.deptId+" union select id from ss_sys_depts where parent_id in(select parent_id from ss_sys_depts where id = "+this.deptId+")";
		if(deptType.equals("")){
			hql = "select id from ss_sys_depts where id="+this.deptId+" union select id from ss_sys_depts where parent_id="+this.deptId;
		}
System.out.println("filterDeptIds===>\n"+hql);
		List list = sysUsersBpo.searchSql(hql);
		StringBuilder builder =new StringBuilder();
		builder.append("(");
		for(int i=0; i<list.size(); ++i){
			if(i != 0)
				builder.append(" or ");
			builder.append("id = ");
			builder.append(list.get(i));
		}
		builder.append(")");
		this.filterIds = builder.toString();
	}

	public String filterDeptLevel(){
		if(deptType.equals("0")){

			return this.deptLevel.substring(0, this.deptLevel.length()-2);
		}else{

			return this.deptLevel;
		}
	}

	public String getFilterByDeptLevel(){
		if(deptType.equals("0")){

			return this.deptLevel.substring(0, this.deptLevel.length()-2);
		}else{

			return this.deptLevel;
		}
	}

	public String getFilterByDeptIds(){
		return this.filterIds;
	}

	public String getDeptCname() {
		return deptCname;
	}

	public void setDeptCname(String deptCname) {
		this.deptCname = deptCname;
	}

	public Long getDeptId() {
		return deptId;
	}

	public boolean hasOptRight(String moduleId,String right){
		if(perms ==null)
			return false;
		return perms.hasOptRight(moduleId, right);
	}

	public boolean hasRight(String moduleId,String cmd){
//		System.out.println("*************************************************");
//		System.out.println("moduleId="+moduleId);
//		System.out.println("perms="+perms);
//		System.out.println("cmd="+cmd);
		if(perms ==null){
			System.out.println("--moduleId="+moduleId+" : cmd="+cmd);
			return false;
		}
		return perms.hasRight(moduleId, cmd);
	}

	public Long getUserId() {
		return userId;
	}
	public void setUserId(Long userId) {
		this.userId = userId;
	}
	public String getUserName() {
		return userName;
	}
	public void setUserName(String userName) {
		this.userName = userName;
	}
	public String getUserCname() {
		return userCname;
	}
	public void setUserCname(String userCname) {
		this.userCname = userCname;
	}
	public String getFullName() {
		return fullName;
	}
	public void setFullName(String fullName) {
		this.fullName = fullName;
	}
	public Permissions getPerms() {
		return perms;
	}
	public void setPerms(Permissions perms) {
		this.perms = perms;
	}

	public void setDeptId(Long deptId) {
		this.deptId = deptId;
	}

	public String getDeptLevel() {
		return deptLevel;
	}

	public void setDeptLevel(String deptLevel) {
		this.deptLevel = deptLevel;
	}

	public String getDeptType() {
		return deptType;
	}

	public void setDeptType(String deptType) {
		this.deptType = deptType;
	}

	public String getInit_password() {
		return init_password;
	}

	public void setInit_password(String init_password) {
		this.init_password = init_password;
	}

	public Integer getLogin_failures() {
		return login_failures;
	}

	public void setLogin_failures(Integer login_failures) {
		this.login_failures = login_failures;
	}

	public Integer getLock_sign() {
		return lock_sign;
	}

	public void setLock_sign(Integer lock_sign) {
		this.lock_sign = lock_sign;
	}

	public Date getPwd_modified_time() {
		return pwd_modified_time;
	}

	public void setPwd_modified_time(Date pwd_modified_time) {
		this.pwd_modified_time = pwd_modified_time;
	}

	public String getCredential_number() {
		return credential_number;
	}

	public void setCredential_number(String credential_number) {
		this.credential_number = credential_number;
	}

	public Date getLogin_time() {
		return login_time;
	}

	public void setLogin_time(Date login_time) {
		this.login_time = login_time;
	}

	@Resource
	public void setSysUsersBpo(SysUsersBpo sysUsersBpo) {
		this.sysUsersBpo = sysUsersBpo;
	}

	public Long getOrgId() {
		return orgId;
	}

	public void setOrgId(Long orgId) {
		this.orgId = orgId;
	}

	public String getOrgname() {
		return orgname;
	}

	public void setOrgname(String orgname) {
		this.orgname = orgname;
	}

	public String getUserType() {
		return userType;
	}

	public void setUserType(String userType) {
		this.userType = userType;
	}

	public Map getOperationMap() {
		return operationMap;
	}

	public void setOperationMap(Map operationMap) {
		this.operationMap = operationMap;
	}


}
